<?php
(!defined('IN_SYSTEM') || !defined('ADMIN_PRE')) && exit('Access Denied!');
$f = isset($_REQUEST['f'])?$_REQUEST['f']:'setting';
if(isset($_G['usergroup']['access']["ec$f"]) && !ican("ec$f")){
	msg('没有权限');
}
$m = new EcommerceModule;
$m->$f();
class EcommerceModule
{
	function setting(){
		global $_G;
		if(getGP('ecmsubmit','P')){
			$ecm=getGP('ecm','P','array');
			$val=encrypt(serialize($ecm));
			$_G['db']->query("DELETE FROM".table('settings')."WHERE name='setting' AND type='ecm'");
			$_G['db']->query("INSERT INTO".table('settings')."(`name`, `value`, `type`) VALUES ('setting', '$val', 'ecm')");
			recache('settings');
			msg('保存成功，你可能需要刷新一下缓存！');
		}else{
			$ecm = unserialize(decrypt($_G['settings']['ecm']['setting']));
			include admintemplate('ecommerce');
		}
	}
	function payment(){
		global $_G;
		if(!getGP('ecmsubmit','P')){
			$type=getGP('type','G');
			if(!$type)$type='alipay';
			
			$ecm = unserialize(decrypt($_G['settings']['payment'][$type]));
			switch($type){
				case 'alipay':
					if( $ecm['acode'] ) $ecm['acode']=substr($ecm['acode'], 0,2).'********'.substr($ecm['acode'], -2,2);
					break;
				case 'tenpay':
					if( $ecm['tcode'] ) $ecm['tcode']=substr($ecm['tcode'], 0,2).'********'.substr($ecm['tcode'], -2,2);
					if( $ecm['tkey'] ) $ecm['tkey']=substr($ecm['tkey'], 0,2).'********'.substr($ecm['tkey'], -2,2);
					break;
				case 'quickpay':
					if( $ecm['code'] ) $ecm['code']=substr($ecm['code'], 0,2).'********'.substr($ecm['code'], -2,2);
					break;
			}
			
			include admintemplate('ecommerce');
		}else{
			$type=getGP('type','p');
			$ecm=getGP('ecm','P','array');
			
			if(empty($ecm) || !$type){
					msg('请填写完全');
			}else{
					$ecmo = unserialize(decrypt($_G['settings']['payment'][$type]));
					$op=false;
					switch($type){
						case 'alipay':
							if( $ecm['acode'] == substr($ecmo['acode'], 0,2).'********'.substr($ecmo['acode'], -2,2) ) $ecm['acode'] = $ecmo['acode'];
							break;
						case 'tenpay':
							if( $ecm['tcode'] == substr($ecmo['tcode'], 0,2).'********'.substr($ecmo['tcode'], -2,2) ) $ecm['tcode'] = $ecmo['tcode'];
							if( $ecm['tkey'] == substr($ecmo['tkey'], 0,2).'********'.substr($ecmo['tkey'], -2,2) ) $ecm['tkey'] = $ecmo['tkey'];
							break;
						case 'quickpay':
							if( $ecm['code'] == substr($ecmo['code'], 0,2).'********'.substr($ecmo['code'], -2,2) ) $ecm['code'] = $ecmo['code'];
							break;
					}
					$val=encrypt(serialize($ecm));
					$_G['db']->query("DELETE FROM".table('settings')."WHERE name='$type' AND type='payment'");
					$_G['db']->query("INSERT INTO".table('settings')."(`name`, `value`, `type`) VALUES ('$type', '$val', 'payment')");
					recache('settings');
					msg('保存成功，你可能需要刷新一下缓存！');
			}
		}
	}
}	
?>